package com.tarena.lbs.content.dao.repository.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.content.dao.mapper.CategoryMapper;
import com.tarena.lbs.content.dao.repository.CategoryRepository;
import com.tarena.lbs.content.pojo.param.ArticleCategoryParam;
import com.tarena.lbs.content.pojo.po.ArticleCategoryPO;
import com.tarena.lbs.content.pojo.query.ArticleCategoryQuery;
import com.tarena.lbs.content.pojo.query.ArticleQuery;
import com.tarena.lbs.content.pojo.vo.ArticleCategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public class CategoryRepositoryImpl implements CategoryRepository {
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public PageInfo<ArticleCategoryPO> pageList(ArticleCategoryQuery query) {
        QueryWrapper<ArticleCategoryPO> queryWrapper = assembleArticleCategoryQuery(query);
        List<ArticleCategoryPO> list =  categoryMapper.selectList(queryWrapper);

        return new PageInfo<>( list);
    }

    private QueryWrapper<ArticleCategoryPO> assembleArticleCategoryQuery(ArticleCategoryQuery query) {
        QueryWrapper<ArticleCategoryPO> queryWrapper = new QueryWrapper<>();

        // 只有当明确指定要包含已删除数据时，才不添加这个条件
        if(query.getIncludeDeleted() == null || !query.getIncludeDeleted()){
            queryWrapper.eq("delete_status", 0);
        }

        if(query.getCategoryName()!=null){
            queryWrapper.eq("category_name",query.getCategoryName());
        }

        // 修改状态查询逻辑：只有当categoryStatus不为null且不等于特定的"全部"标识值时才添加状态筛选
        // 假设前端在选择"全部"时可能传递了一个特殊值，如-1，我们需要排除这个情况
        if(query.getCategoryStatus()!=null && query.getCategoryStatus() != -1){
            queryWrapper.eq("category_status",query.getCategoryStatus());
        }

        queryWrapper.orderByDesc("id");

        return queryWrapper;
    }
    @Override
    public Result<Void> addCategory(ArticleCategoryParam param) {
        ArticleCategoryPO po = new ArticleCategoryPO();
        po.setCategoryName(param.getCategoryName());
        po.setSort(param.getSort());
        po.setCategoryStatus(param.getCategoryStatus());
        categoryMapper.insert(po);
        return Result.success(); // 添加返回值
    }

    @Override
    public void updateCategory(ArticleCategoryPO poParam) {
        categoryMapper.updateById(poParam);
    }

    @Override
    public void deleteCategory(Integer id) {
        // 假删除：将删除状态设置为1
        ArticleCategoryPO categoryPO = new ArticleCategoryPO();
        categoryPO.setId(id);
        categoryPO.setDeleteStatus(1); // 1表示已删除状态
        categoryPO.setUpdateTime(new Date());

        // 使用updateById方法更新记录
        categoryMapper.updateById(categoryPO);
    }

    @Override
    public ArticleCategoryPO getById(Integer id) {
        if (id == null || id <= 0) {
            return null;
        }
        return categoryMapper.selectById(id);
    }
}